class_name BoarEnemy
extends Enemy

@onready var wall_checker: RayCast2D = $Graphics/WallChecker
@onready var player_checker: RayCast2D = $Graphics/PlayerChecker
@onready var floor_checker: RayCast2D = $Graphics/FloorChecker


func _ready():
	status_machine.blackboard = blackboard
	status_machine.actor = self
	status_machine.load_child()
	status_machine.start_status_node("Idle")
	enemy_status_pannel.load(blackboard.get_stats())
 
func _process(delta: float) -> void:
	status_machine.run_process(delta)

func _physics_process(delta: float) -> void:
	status_machine.run_physics_process(delta)

func play_animation(name:String) ->void:
	name = name.to_lower()
	animation_player.play(name)
	
func is_play_animation(name) -> bool:
	return animation_player.current_animation == name.to_lower() && animation_player.is_playing()

func _on_hurtbox_hurt(hitbox: Hitbox) -> void:
	Game.slow_motion_short()
	var pending_damage:Damage = blackboard.create_damage(hitbox.owner, 1)
	update_health(pending_damage.amount)
		
func hurt():
	var pending_damage:Damage = blackboard.create_damage(null, 1, 2)
	update_health(pending_damage.amount)
	
func update_health(v:int):
	var new_health = blackboard.update_health(v)
	if new_health <= 0:
		status_machine.change_status("Death")
	else:
		status_machine.change_status("Hurt")


func _on_hitbox_hit(hurtbox: Hurtbox) -> void:
	var player:Player = hurtbox.owner
	var dir = global_position.direction_to(player.global_position)
	face_direction( 1 if dir.x > 0 else -1 )
	
func face_direction(v) -> void:	# 转向
	blackboard.direction = v
	graphics.scale.x = -v
